/**
 * 封装uni的request
 */
//const baseUrl = 'http://127.0.0.1:8001'
const baseUrl = 'https://www.houselanlanli.com/api'
//const baseUrl = 'https://lanlanli.lemangweb.com/api'
//const baseUrl = 'http://manka-lanlanli.com/api'


import { setToken,getToken, removeToken } from "./auth";


export function request(url, method = "GET", data,config={}) {
  return new Promise(function (resolve, reject) {
		let token = uni.getStorageSync('token')
		console.log(url,'url')
		let header = {
		  //设置请求头信息
		  'Authorization': getToken(),
		  'X-Requested-With': 'XMLHttpRequest',
		  "Accept": "application/json",
		  "Content-Type": "application/json; charset=UTF-8"
		};
		console.log('头部信息')
		console.log(header)
    uni.request({
      url:baseUrl+ url,
      data: data,
      method: method,
	  //header:header,
      header: {
        'Content-Type': 'application/json',
        //'Content-Type': 'application/x-www-form-urlencoded',
		"Accept": "application/json",
        'Cookie': 'JSESSIONID='+ wx.getStorageSync('sessionId'),
		'Token': getToken(),
      },
      success: function (res) {
		  console.log("返回结果");
		  console.log(res);
		  
        uni.hideLoading()
		if(res.data.code==-1) {
			uni.removeStorageSync('token')
			uni.navigateTo({
				url:'/pages/login/login'
			})
			return 
		}
        resolve(res.data)
      },
      fail: function (err) {
       let msg=JSON.stringify(err)
        uni.showModal({
          title: '错误',
          // content: '网络异常',
          content:msg,
          showCancel: false
        });
        reject(err);
      }
    })
  });
}



export function request_wx(url, method = "GET", data,config={}) {
	  uni.request({
		url:baseUrl+ url,
		data: data,
		method: method,
		//header:header,
		header: {
		  'Content-Type': 'application/json',
		  //'Content-Type': 'application/x-www-form-urlencoded',
		"Accept": "application/json",
		  'Cookie': 'JSESSIONID='+ wx.getStorageSync('sessionId'),
		'Token': getToken(),
		},
		success: function (res) {
		  console.log("返回结果");
		  console.log(res);
		  
		  uni.hideLoading()
		if(res.data.code==-1) {
			uni.removeStorageSync('token')
			uni.navigateTo({
				url:'/pages/login/login'
			})
			return 
		}
		  resolve(res.data)
		},
		fail: function (err) {
		 let msg=JSON.stringify(err)
		  uni.showModal({
			title: '错误',
			// content: '网络异常',
			content:msg,
			showCancel: false
		  });
		  reject(err);
		}
	  })
}


